﻿#region using statements

using System.Collections.Generic;
using Microsoft.Xna.Framework;

#endregion

namespace XNAGameEngine.GUI
{
    /// <summary>
    /// This abstract class represents a single GUI layout. Inherit this class to create a GUI layout. 
    /// It can then be loaded in the ManagerGUI.
    /// </summary>
    public abstract class GUILayout
    {
        #region local fields

        /// <summary>
        /// The GUIManager this layout belongs to
        /// </summary>
        protected readonly IManagerGUI _guiManager;

        /// <summary>
        /// The string associated with this layout
        /// </summary>
        private readonly string _name;

        /// <summary>
        /// The list of objects in this layout
        /// </summary>
        protected List<GUIObject> _objectList;

        #endregion

        #region properties

        /// <summary>
        /// Returns the name of this layout
        /// </summary>
        public string Name
        {
            get { return _name; }
        }

        #endregion

        #region constructors

        /// <summary>
        /// Create a new GUILayout
        /// </summary>
        /// <param name="guiManager">the GUIManager this layout belongs to</param>
        public GUILayout(IManagerGUI guiManager, string name)
        {
            _guiManager = guiManager;
            _name = name;
            _objectList = new List<GUIObject>();
        }

        #endregion

        #region local methods

        /// <summary>
        /// <para>Initialize the layout.</para>
        /// <para>This is where the GUIObjects should be added to the list</para>
        /// </summary>
        public virtual void Initialize()
        { }

        /// <summary>
        /// Cleanup the layout.
        /// </summary>
        public virtual void Cleanup()
        {
            foreach (GUIObject obj in _objectList)
                obj.Cleanup();
            _objectList.Clear();
        }

        /// <summary>
        /// <para>Update the layout.</para>
        /// <para>This is where all of the GUIObjects will get Update() called</para>
        /// <para>When overriding this method, be sure to call base.Update() to have the GUIOjects
        /// updated.</para>
        /// </summary>
        /// <param name="gameTime">Snapshot of timing values</param>
        public virtual void Update(GameTime gameTime)
        {
            foreach (GUIObject obj in _objectList)
                obj.Update(gameTime);
        }

        /// <summary>
        /// <para>Draw the layout.</para>
        /// <para>This is where all of the GUIObjects will get Draw() called.</para>
        /// <para>When overriding this method, be sure to call base.Draw() to have the GUIOjects
        /// drawn to the screen.</para>
        /// </summary>
        /// <param name="gameTime">Snapshot of timing values</param>
        public virtual void Draw(GameTime gameTime)
        {
            GameEngine.SpriteBatch.Begin();

            foreach (GUIObject obj in _objectList)
                obj.Draw(gameTime);

            GameEngine.SpriteBatch.End();
        }

        #endregion
    }
}